package com.pda.yuanjing.pages.outbill;

import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pda.yuanjing.PDApp;
import com.pda.yuanjing.R;
import com.pda.yuanjing.beanout.DownBillBeanOut;
import com.pda.yuanjing.boardcast.CustomReceiver;
import com.pda.yuanjing.boardcast.ScannerInterface;
import com.pda.yuanjing.pages.inbill.InBillActivity;
import com.pda.yuanjing.pages.inbill.InBillAdapter;
import com.pda.yuanjing.pages.outbill.scanf.OutScanfActivity;
import com.public_module.basemvp.activity.BaseActivity;
import com.public_module.tools.VoiceUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OutBillActivity extends BaseActivity<OutBillModel, OutBillView, OutBillPresent> implements OutBillView {
    private String TAG = "InBillActivity";

    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rltitle)
    RelativeLayout rltitle;
    @BindView(R.id.login_id)
    EditText loginId;
    @BindView(R.id.loginbt)
    Button loginbt;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    OutBillAdapter outBillAdapter;
    List<DownBillBeanOut.DataBean> listdata = new ArrayList<>();
    List<DownBillBeanOut.DataBean> listdataTemp = new ArrayList<>();

    ScannerInterface scanner;
    CustomReceiver customReceiver = null;
    VoiceUtils voiceUtils;

    @Override
    public OutBillModel createModel() {
        return new OutBillModelImpl(this);
    }

    @Override
    public OutBillView createView() {
        return this;
    }

    @Override
    public OutBillPresent createPresenter() {
        return new OutBillPresent();
    }

    @Override
    public void showlist(final List<DownBillBeanOut.DataBean> list) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                listdataTemp.clear();
                listdata = list;
                for (int i = 0; i <listdata.size() ; i++) {
                    listdataTemp.add(listdata.get(i));
                }
                outBillAdapter = new OutBillAdapter(OutBillActivity.this, listdataTemp);
                listview.setAdapter(outBillAdapter);
                loginId.setText("");
                refreshLayout.finishRefresh();
            }
        });
    }

    @Override
    public void tip(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showMsg(str);
                loginId.setText("");
                refreshLayout.finishRefresh();
            }
        });
    }

    protected void initdata() {
        tvTitle.setText("出库订单");
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setEnableHeaderTranslationContent(true);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                presenter.showlist("");
            }
        });
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                outBillAdapter.setSelectedPosition(position);
                outBillAdapter.notifyDataSetInvalidated();
                intentToActivityWithStringParameter(OutBillActivity.this, OutScanfActivity.class, PDApp.billID,listdataTemp.get(position).getBillID());
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_outbill);
        ButterKnife.bind(this);
        initdata();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        scanner = new ScannerInterface(this);
        scanner.setOutputMode(1);
        customReceiver = new CustomReceiver(
                new CustomReceiver.PListener1() {
                    @Override
                    public void setData(CustomReceiver.Dataa dataa) {
                    }
                },
                new CustomReceiver.PListener2() {
                    @Override
                    public void setText(String string) {
                        loginId.setText(string);
                        listdataTemp.clear();
                        if (loginId.getText().toString().length() == 0) {
                            listdataTemp.addAll(listdata);
                        }else{
                            for (int i = 0; i <listdata.size() ; i++) {
                                if(listdata.get(i).getBillCode().contains(loginId.getText().toString())){
                                    listdataTemp.add(listdata.get(i));
                                }
                            }
                        }
                        outBillAdapter = new OutBillAdapter(OutBillActivity.this, listdataTemp);
                        listview.setAdapter(outBillAdapter);
                        if(listdataTemp.size()==1){
                            intentToActivityWithStringParameter(OutBillActivity.this, OutScanfActivity.class, PDApp.billID,listdataTemp.get(0).getBillID());
                        }
                    }
                });
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        filter.addAction(ScannerInterface.RES_ACTION);
        registerReceiver(customReceiver, filter);
        voiceUtils = new VoiceUtils(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (customReceiver != null) {
            unregisterReceiver(customReceiver);
        }
        if (scanner != null) {
            scanner.setOutputMode(0);
        }
    }

    @OnClick({R.id.iv_title_left, R.id.loginbt,R.id.tv_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.tv_right:
                presenter.showlist("");
                break;
            case R.id.loginbt:
                listdataTemp.clear();
                if (loginId.getText().toString().length() == 0) {
                    listdataTemp.addAll(listdata);
                }else{
                    for (int i = 0; i <listdata.size() ; i++) {
                        if(listdata.get(i).getBillCode().contains(loginId.getText().toString())){
                            listdataTemp.add(listdata.get(i));
                        }
                    }
                }
                outBillAdapter = new OutBillAdapter(OutBillActivity.this, listdataTemp);
                listview.setAdapter(outBillAdapter);
                break;
        }
    }
}
